﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
using System.Data.SqlClient;
using System.Data.Entity;
using Orange.Logistics.Models;

namespace Orange.Logistics.Services {
    public interface IPermissionService : IBaseService<Permission> {
        bool CheckRight(int[] roles, string controller, string action);
    }

    public class PermissionService : BaseService<Permission>, IPermissionService {
        public bool CheckRight(int[] roles, string controller, string action) {
            var query = (from r in db.Roles
                         join rp in db.RolePermissions on r.Id equals rp.RoleID
                         join p in db.Permissions on rp.PermissionID equals p.Id
                         where roles.Contains(r.Id)
                         select p.Id).Distinct();

            return query.Count() > 0;
        }      
    }
}
